package com.ruoyi.system.service.impl;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.math.BigDecimal;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.domain.BizPurchaseReturn;
import com.ruoyi.system.mapper.BizPurchaseReturnMapper;
import com.ruoyi.system.service.IBizPurchaseReturnService;
import com.ruoyi.system.mapper.BizInspectionPlanMapper;
import com.ruoyi.system.domain.BizInspectionPlan;

@Service
public class BizPurchaseReturnServiceImpl implements IBizPurchaseReturnService {

    @Autowired
    private BizPurchaseReturnMapper bizPurchaseReturnMapper;

    @Autowired
    private BizInspectionPlanMapper bizInspectionPlanMapper;

    @Override
    public BizPurchaseReturn selectBizPurchaseReturnByReturnId(Long returnId) {
        return bizPurchaseReturnMapper.selectBizPurchaseReturnByReturnId(returnId);
    }

    @Override
    public List<BizPurchaseReturn> selectBizPurchaseReturnList(BizPurchaseReturn bizPurchaseReturn) {
        return bizPurchaseReturnMapper.selectBizPurchaseReturnList(bizPurchaseReturn);
    }

    @Override
    public int insertBizPurchaseReturn(BizPurchaseReturn bizPurchaseReturn) {
        if (StringUtils.isEmpty(bizPurchaseReturn.getReturnNo())) {
            bizPurchaseReturn.setReturnNo(getNextReturnNoForToday());
        }
        bizPurchaseReturn.setDelFlag("0");
        bizPurchaseReturn.setStatus(StringUtils.defaultString(bizPurchaseReturn.getStatus(), "DRAFT"));
        bizPurchaseReturn.setCreatedTime(new Date());
        return bizPurchaseReturnMapper.insertBizPurchaseReturn(bizPurchaseReturn);
    }

    @Override
    public int updateBizPurchaseReturn(BizPurchaseReturn bizPurchaseReturn) {
        bizPurchaseReturn.setUpdatedTime(new Date());
        return bizPurchaseReturnMapper.updateBizPurchaseReturn(bizPurchaseReturn);
    }

    @Override
    public int deleteBizPurchaseReturnByReturnIds(Long[] returnIds) {
        return bizPurchaseReturnMapper.deleteBizPurchaseReturnByReturnIds(returnIds);
    }

    @Override
    public int deleteBizPurchaseReturnByReturnId(Long returnId) {
        return bizPurchaseReturnMapper.deleteBizPurchaseReturnByReturnId(returnId);
    }

    @Override
    public String getNextReturnNoForToday() {
        String prefix = new SimpleDateFormat("yyyyMMdd").format(new Date());
        String max = bizPurchaseReturnMapper.selectMaxReturnNoByDate(prefix);
        int next = 1;
        if (StringUtils.isNotEmpty(max)) {
            try {
                String tail = max.substring(prefix.length());
                next = Integer.parseInt(tail) + 1;
            } catch (Exception ignored) {}
        }
        return prefix + String.format("%03d", next);
    }

    @Override
    public BizPurchaseReturn generateFromInspectionPlan(String planNo, BigDecimal quantityOverride, String reasonOverride, Long barcodeId) {
        // 依据 planNo 查找检验计划
        BizInspectionPlan query = new BizInspectionPlan();
        query.setPlanNo(planNo);
        java.util.List<BizInspectionPlan> plans = bizInspectionPlanMapper.selectBizInspectionPlanList(query);
        if (plans == null || plans.isEmpty()) {
            throw new IllegalArgumentException("检验计划不存在: " + planNo);
        }
        BizInspectionPlan plan = plans.get(0);

        BizPurchaseReturn ret = new BizPurchaseReturn();
        ret.setReturnNo(getNextReturnNoForToday());
        ret.setSupplierCode(plan.getSupplierCode());
        ret.setSupplierName(plan.getSupplierName());
        ret.setInventoryCode(plan.getInventoryCode());
        ret.setInventoryName(plan.getInventoryName());
        ret.setBatchNo(plan.getBatchNo());
        ret.setReturnQuantity(quantityOverride != null ? quantityOverride : plan.getRejectedQuantity());
        ret.setReason(StringUtils.isNotEmpty(reasonOverride) ? reasonOverride : plan.getRejectionReason());
        ret.setBarcodeId(barcodeId);
        ret.setReceiptNoteNo(plan.getReceiptNoteNo());
        ret.setInspectionPlanNo(plan.getPlanNo());
        ret.setStatus("DRAFT");
        ret.setDelFlag("0");
        ret.setCreatedTime(new Date());

        bizPurchaseReturnMapper.insertBizPurchaseReturn(ret);
        return ret;
    }
}